<?php
namespace app\manage\controller;

use app\manage\controller\Base;
use app\model\LogModel;
use app\model\SystemModel;
use app\service\AdminService;
use think\facade\Db;
use support\Request;
use support\View;
use Webman\Captcha\CaptchaBuilder;


use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\ValidationException;
class LoginController extends Base
{
    public function index(Request $request)
    {
        if($request->method()=='POST'){
            $captcha = $request->post('coder');
            if (strtolower($captcha) !== $request->session()->get('captcha')) {
                return view_e('验证码不正确');
            }
            try {
                $data = v::input($request->post(), [
                'username' => v::NotEmpty()->setName('用户名'),
                'password' => v::NotEmpty()->setName('密码')
                ]);
            } catch (ValidationException $e) {
                return view_e($e->getMessage());
            }
           	$ip=$request->getRealIp($safe_mode=true);
			//进行解密
            $password=base64_encode($data['password'].'lizhili');
			$data['username']=$data['username'];
			$data['password']=$data['password'];
			$login=new AdminService();
			$info=$login->login($data);
			if($info){
				return view_s('信息正确，正在跳转',url('index/index'));
			}else{
                return view_e('用户名或密码错误');
			}
    	}
        $system=SystemModel::where('enname','webname')->value('value');
        View::assign('system',$system);
        return view('login/index');
    }
	public function out()
    {
        session()->delete('uname');
        session()->delete('uid');
		return view_s('退出成功',url('login/index'));
    }

    /**
     * 输出验证码图像
     */
    public function captcha(Request $request)
    {
        // 初始化验证码类
        $builder = new CaptchaBuilder;
        $builder->setBackgroundColor(255,255,255);

        // 生成验证码
        $builder->build();
        // 将验证码的值存储到session中
        $request->session()->set('captcha', strtolower($builder->getPhrase()));
        // 获得验证码图片二进制数据
        $img_content = $builder->get();
        // 输出验证码二进制数据
        return response($img_content, 200, ['Content-Type' => 'image/jpeg']);
    }
    public function log(Request $request){

        $data=$request->get();
        View::assign('key', $data);
        if(isset($data['start']) or isset($data['end'])){
            $map=[];
            if($data['start'] != ''){
                $map[]=	['create_time','>',strtotime($data['start'])];
            }
            if($data['end'] != ''){
                $map[]=	['create_time','<',strtotime($data['end'])+60*60*24];
            }
        }else{
            $map=true;
        }
        $data=Db::name('log')->where($map)->order('id','desc')->paginate(['list_rows'=>15,'query'=>request()->all()]);
        View::assign('data',$data);
        $count1=Db::name('log')->count();
        View::assign('count1', $count1);
        return view('login/log');
    }
    public function ajax(Request $request)
    {
        $data=$request->post();
        if($data['type']=='log_del'){
            if(Db::name('log')->delete($data['id'])){
                return 1;//修改成功返回1
            }else{
                return 0;
            }
        }
        if($data['type']=='log_all'){
            if(Db::name('log')->delete($data['id'])){
                return 1;//修改成功返回1
            }else{
                return 0;
            }
        }
        return 0;
    }

}
